home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / siggraphCD / lib / libaux / xform.c < prev   
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.2 KB  |  165 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <math.h>
  38. #include <stdio.h>
  39. #include <GL/gl.h>
  40.  
  41. #if defined(__cplusplus) || defined(c_plusplus)
  42. #define class c_class
  43. #endif
  44.  
  45. #define STACKDEPTH 10
  46.  
  47. typedef struct {
  48.     GLdouble    mat[4][4];
  49.     GLdouble    norm[3][3];
  50. } mat_t;
  51.  
  52. static mat_t matstack[STACKDEPTH] = {
  53.     {{{1.0, 0.0, 0.0, 0.0},
  54.     {0.0, 1.0, 0.0, 0.0},
  55.     {0.0, 0.0, 1.0, 0.0},
  56.     {0.0, 0.0, 0.0, 1.0}},
  57.     {{1.0, 0.0, 0.0},
  58.     {0.0, 1.0, 0.0},
  59.     {0.0, 0.0, 1.0}}}
  60. };
  61. static int identitymat = 1;
  62.  
  63. static int mattop = 0;
  64.  
  65. void m_xformpt(GLdouble pin[3], GLdouble pout[3], 
  66.     GLdouble nin[3], GLdouble nout[3])
  67. {
  68.     int    i;
  69.     GLdouble    ptemp[3], ntemp[3];
  70.     mat_t    *m = &matstack[mattop];
  71.  
  72.     if (identitymat) {
  73.     for (i = 0; i < 3; i++) {
  74.         pout[i] = pin[i];
  75.         nout[i] = nin[i];
  76.     }
  77.     return;
  78.     }
  79.     for (i = 0; i < 3; i++) {
  80.     ptemp[i] = pin[0]*m->mat[0][i] +
  81.            pin[1]*m->mat[1][i] +
  82.            pin[2]*m->mat[2][i] +
  83.            m->mat[3][i];
  84.     ntemp[i] = nin[0]*m->norm[0][i] +
  85.            nin[1]*m->norm[1][i] +
  86.            nin[2]*m->norm[2][i];
  87.     }
  88.     for (i = 0; i < 3; i++) {
  89.     pout[i] = ptemp[i];
  90.     nout[i] = ntemp[i];
  91.     }
  92.     normalize(nout);
  93. }
  94.  
  95. void m_xformptonly(GLdouble pin[3], GLdouble pout[3])
  96. {
  97.     int    i;
  98.     GLdouble    ptemp[3];
  99.     mat_t    *m = &matstack[mattop];
  100.  
  101.     if (identitymat) {
  102.     for (i = 0; i < 3; i++) {
  103.         pout[i] = pin[i];
  104.     }
  105.     return;
  106.     }
  107.      for (i = 0; i < 3; i++) {
  108.     ptemp[i] = pin[0]*m->mat[0][i] +
  109.            pin[1]*m->mat[1][i] +
  110.            pin[2]*m->mat[2][i] +
  111.            m->mat[3][i];
  112.     }
  113.     for (i = 0; i < 3; i++) {
  114.     pout[i] = ptemp[i];
  115.     }
  116. }
  117.  
  118. void m_pushmatrix(void)
  119. {
  120.     if (mattop < STACKDEPTH-1) {
  121.     matstack[mattop+1] = matstack[mattop];
  122.     mattop++;
  123.     } else
  124.     error("m_pushmatrix: stack overflow\n");
  125. }
  126.  
  127. void m_popmatrix(void)
  128. {
  129.     if (mattop > 0)
  130.     mattop--;
  131.     else
  132.     error("m_popmatrix: stack underflow\n");
  133. }
  134.  
  135. void m_translate(GLdouble x, GLdouble y, GLdouble z)
  136. {
  137.     int    i;
  138.     mat_t    *m = &matstack[mattop];
  139.  
  140.     identitymat = 0;
  141.     for (i = 0; i < 4; i++)
  142.     m->mat[3][i] = x*m->mat[0][i] +
  143.                  y*m->mat[1][i] +
  144.                  z*m->mat[2][i] +
  145.                  m->mat[3][i];
  146. }
  147.  
  148. void m_scale(GLdouble x, GLdouble y, GLdouble z)
  149. {
  150.     int    i;
  151.     mat_t    *m = &matstack[mattop];
  152.  
  153.     identitymat = 0;
  154.     for (i = 0; i < 3; i++) {
  155.     m->mat[0][i] *= x;
  156.     m->mat[1][i] *= y;
  157.     m->mat[2][i] *= z;
  158.     }
  159.     for (i = 0; i < 3; i++) {
  160.     m->norm[0][i] /= x;
  161.     m->norm[1][i] /= y;
  162.     m->norm[2][i] /= z;
  163.     }
  164. }
  165.